import { onMounted, ref } from "vue";
import {
  getCommunityListApi,
  getRoleListApi,
  buildingListApi,
  memberListApi,
  cateListApi,
} from "@/api/HJhook/hook.js";

//角色状态、启用状态    0：禁用  1：启用
export const useUserState = (needAll = false) => {
  let stateList = [
    { label: "启用", value: 1 },
    { label: "禁用", value: 0 },
  ];
  return stateList;
};

//角色
export const useRoleList = () => {
  const roleList = ref([]);
  // 全部获取角色
  const getAllRoleList = async () => {
    const res = await getRoleListApi();
    roleList.value = res.data;
  };
  onMounted(() => {
    getAllRoleList();
  });
  return roleList;
};

//获取所有小区
export const useCommunityAllList = () => {
  const communityList = ref([]);
  // 定义方法
  const getAllCommunityList = async () => {
    let res = await getCommunityListApi();
    communityList.value = res.data;
  };
  onMounted(() => {
    getAllCommunityList();
  });
  return communityList;
};

// 获取栋数
export const useBuildingList = (code) => {
  const buildingList = ref([]);
  // 全部获取栋数
  const getAllBuildingList = async () => {
    const res = await buildingListApi(code);
    buildingList.value = res.data;
  };
  onMounted(() => {
    getAllBuildingList();
  });
  return buildingList;
};

//性别  1：男   0：女
export const useGenderState = (needAll = false) => {
  let genderList = [
    { label: "男", value: 1 },
    { label: "女", value: 0 },
  ];
  return genderList;
};

//成员类型  1：业主  2：家庭成员  3：租客
export const useMemberStatus = () => {
  const memberStatus = [
    { label: "业主", value: 1 },
    { label: "家庭成员", value: 2 },
    { label: "租客", value: 3 },
  ];
  return memberStatus;
};
// 获取成员列表
export const useMemberList = () => {
  const memberList = ref([]);
  // 全部获取成员列表
  const getAllMemberList = async () => {
    const res = await memberListApi();
    memberList.value = res.data;
  };
  onMounted(() => {
    getAllMemberList();
  });
  return memberList;
};

// 获取活动状态
export const useActivityStatus = () => {
  const activityStatus = [
    { label: "有效", value: 1 },
    { label: "无效", value: 2 },
  ];
  return activityStatus;
};

// 获取保修状态
export const useRepairStatus = () => {
  const repairStatus = [
    { label: "待受理", value: 0 },
    { label: "已受理", value: 1 },
    { label: "已维修", value: 2 },
  ];
  return repairStatus;
};
// 投诉管理状态
export const useComplainStatus = () => {
  const complainStatus = [
    { label: "不匿名", value: 1 },
    { label: "匿名", value: 0 },
  ];
  return complainStatus;
};
// 信箱管理状态
export const useMailboxStatus = () => {
  const mailboxStatus = [
    { label: "已读回复", value: 1 },
    { label: "未读", value: 0 },
  ];
  return mailboxStatus;
};
// 广告位置
export const useAdvStatus = () => {
  const advStatus = [
    { label: "移动端轮播图", value: 1 },
    { label: "PC端轮播图", value: 2 },
  ];
  return advStatus;
};

// 文章状态
export const useArticleStatus = () => {
  const articleStatus = [
    {
      label: "隐藏",
      value: 0,
    },
    {
      label: "显示",
      value: 1,
    },
  ];
  return articleStatus;
};
// 通知公告类型
export const useNoticeStatus = () => {
  const noticeStatus = [
    { label: "通知", value: 1 },
    { label: "公告", value: 2 },
  ];
  return noticeStatus;
};
// 投票管理类型
export const useTicketStatus = () => {
  const ticketStatus = [
    { label: "单选", value: 0 },
    { label: "多选", value: 1 },
  ];
  return ticketStatus;
};

// 资讯管理 分类名
export const useCateList = () => {
  const cateList = ref([]);
  // 全部获取资讯管理角色
  const getAllCateList = async () => {
    const res = await cateListApi();
    cateList.value = res.data;
  };
  onMounted(() => {
    getAllCateList()
  })
  return cateList
}

// ========= 年 月 日 - 时 分 秒=======
export const formatTimeAll = timestamp => {
  const date = new Date(timestamp);

  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
  }
  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// ===========  年 月 日  ==========
export const formatTime = (timestamp) => {
  // 创建一个Date对象，参数为时间戳
  const date = new Date(timestamp);

  // 使用Date对象提供的方法获取年、月、日、小时、分钟和秒
  const year = date.getFullYear();
  const month = date.getMonth() + 1; //月份是从0开始的，需要加1
  const day = date.getDate();

  return `${year}-${month < 10 ? "0" + month : month}-${day < 10 ? "0" + day : day
    }`;
};
